<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>进度条滚动</title>
  <style>
    #triangle {
      width: 0;
      height: 0;
      border: 30px solid transparent;
      border-bottom-color: #000;
      position: fixed;
      bottom: 20px;
      right: 20px;
    }
  </style>
</head>

<body>
  <div id="body">
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
    <h1>Scroll me</h1>
  </div>
  <!-- 回到顶部 -->
  <div id="triangle" hidden></div>
  <script>
    const triangle = document.querySelector('#triangle')
    const html = document.documentElement
    document.addEventListener('scroll', function () {
      // console.log('ClientRectBottom:',document.documentElement.getBoundingClientRect().bottom);
      // console.log('clientHeight:',document.documentElement.clientHeight);
      // 是否滚动到底部（浏览器窗口顶部到元素底部的距离-浏览器窗口的距离）
      if (html.getBoundingClientRect().bottom - html.clientHeight < 50) {
        const h1 = document.createElement("h1")
        h1.innerHTML = new Date()
        body.appendChild(h1);
        // console.log('到底了');
      }
      // 一定要写在滚动事件里，随着位置的改变，结果也进行改变
      triangle.hidden = !(html.scrollTop > html.clientHeight)
    }, false)
    triangle.addEventListener('click', function () {
      html.scrollTo({
        top: 0,
        behavior: "smooth"
      })
    }, false)
  </script>
</body>

</html>